package com.jmd.ui.tab.a_mapview.frame;

import com.jmd.common.StaticVar;
import com.jmd.entity.setting.AddedLayerSetting;
import com.jmd.rx.TOPIC;
import com.jmd.rx.service.InnerMqService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.Serial;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

@Component
public class AddLayerFrame extends JFrame {

    @Serial
    private static final long serialVersionUID = 6036357857173849529L;

    @Autowired
    private InnerMqService innerMqService;

    private JTextField nameInputTitleTextField;
    private JTextArea nameInputValueTextArea;

//    public AddLayerFrame() {
//        init();
//    }

    @PostConstruct
    private void init() {

        var nameInputTitleLabel = new JLabel("标题：");
        nameInputTitleLabel.setFont(StaticVar.FONT_SourceHanSansCNNormal_12);

        this.nameInputTitleTextField = new JTextField();

        var nameInputValueLabel = new JLabel("地址：");
        nameInputValueLabel.setFont(StaticVar.FONT_SourceHanSansCNNormal_12);

        var nameInputTipLabel = new JLabel("请严格按照{x}{y}{z}的格式填写下载地址");
        nameInputTipLabel.setFont(StaticVar.FONT_SourceHanSansCNNormal_12);

        var nameInputValueScrollPane = new JScrollPane();
        this.nameInputValueTextArea = new JTextArea();
        nameInputValueScrollPane.setViewportView(nameInputValueTextArea);

        var nameInputOkButton = new JButton("确定");
        nameInputOkButton.setFont(StaticVar.FONT_SourceHanSansCNNormal_13);
        nameInputOkButton.setFocusable(false);
        nameInputOkButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                saveNewLayer();
            }
        });

        var groupLayout = new GroupLayout(this.getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(nameInputTitleLabel, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(nameInputTitleTextField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                                        .addComponent(nameInputValueLabel, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(nameInputTipLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                                        .addComponent(nameInputValueScrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                                        .addComponent(nameInputOkButton, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap())
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(nameInputTitleLabel)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(nameInputTitleTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(nameInputValueLabel)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(nameInputTipLabel)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(nameInputValueScrollPane, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(nameInputOkButton)
                                .addContainerGap())
        );
        this.getContentPane().setLayout(groupLayout);

        this.setTitle("添加自定义图层");
        this.setSize(new Dimension(360, 300));
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
                (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
        this.setVisible(false);
        this.setResizable(false);
        this.subInnerMqMessage();

    }

    private void subInnerMqMessage() {
        var client = this.innerMqService.createConnect("AddLayerFrame");
        client.sub(TOPIC.UPDATE_UI, (res) -> {
            SwingUtilities.invokeLater(() -> {
                SwingUtilities.updateComponentTreeUI(this);
            });
        });
    }

    public void inputNewLayer() {
        this.setVisible(true);
    }

    private void saveNewLayer() {
        String title = this.nameInputTitleTextField.getText();
        String value = this.nameInputValueTextArea.getText();
        if (title == null || "".equals(title)) {
            JOptionPane.showMessageDialog(this, "标题不能为空");
            return;
        }
        if (value == null || "".equals(value)) {
            JOptionPane.showMessageDialog(this,"地址不能为空");
            return;
        }
        var result = new AddedLayerSetting();
        result.setTitle(title);
        result.setValue(value);
        this.innerMqService.pub(TOPIC.ADD_NEW_LAYER, result);
        this.nameInputTitleTextField.setText(null);
        this.nameInputValueTextArea.setText(null);
        this.setVisible(false);
    }

}
